package com.zxx.pattern.proxyPattern.aidl;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.zxx.designpattern.R;

public class BankActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvMsg;

    //private BankBinder mBankBinder;
    private IBankAIDL mBankBinder;


    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //假如不同进程 报yxia下错误
            //java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.zxx.pattern.proxyPattern.aidl.BankBinder
            //mBankBinder = (BankBinder) service;

            mBankBinder = IBankAIDL.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank);

        //显示调用服务意图
        Intent intent = new Intent(this, BankService.class);

        //隐式
        //Intent intent = new Intent("com.zxx.aidl.bank.BankService");
        //intent.setPackage(getPackageName());    //需要添加包名

        bindService(intent, connection, BIND_AUTO_CREATE);


        tvMsg = findViewById(R.id.tv_show);
        init(R.id.btn_open);
        init(R.id.btn_save);
        init(R.id.btn_take);
        init(R.id.btn_close);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }

    private void init(int resId) {
        Button button = findViewById(resId);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_open:

                try {
                    tvMsg.setText(mBankBinder.openAccount("Aige", "123"));
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_save:
                try {
                    tvMsg.setText(mBankBinder.saveMoney(55555, "asdasdasdasdasd000"));
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_take:
                try {
                    tvMsg.setText(mBankBinder.takeMoney(250, "asdasdasdsad", "123"));
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_close:
                try {
                    tvMsg.setText(mBankBinder.closeAccount("asdasfdafas", "123"));
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
        }
    }


}